<template>
    <div class="box">
        <div>
            <h1>商户注册</h1>
        </div>
        <div>
            <el-form
                ref="ruleFormRef"
                style="max-width: 600px"
                :model="ruleForm"
                :rules="rules"
                label-width="auto"
            >
                <el-form-item label="登录账号" prop="userName">
                    <el-input v-model="ruleForm.userName" />
                </el-form-item>
                <el-form-item label="登录密码" prop="userPass">
                    <el-input v-model="ruleForm.userPass" />
                </el-form-item>
                <el-form-item label="真实姓名" prop="nickName">
                    <el-input v-model="ruleForm.nickName" />
                </el-form-item>
                <el-form-item label="手机号" prop="userMobile">
                    <el-input v-model="ruleForm.userMobile" />
                </el-form-item>
                <el-form-item label="邮箱" prop="userEmail">
                    <el-input v-model="ruleForm.userEmail" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">
                        注册
                    </el-button>
                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { userReg } from "@/api/api"
import { useRouter } from 'vue-router';


const router = useRouter();

interface RuleForm {
    userName: string;
    userPass: string;
    nickName: string;
    userMobile: string;
    userEmail: string;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
    userName: "",
    userPass: "",
    nickName: "",
    userMobile: "",
    userEmail: "",
});

const locationOptions = ["Home", "Company", "School"];

const rules = reactive<FormRules<RuleForm>>({
    userName: [
        {
            required: true,
            message: "请输入登录账号",
            trigger: "blur",
        },
        { min: 5, max: 16, message: "账号必须是5到16个字符", trigger: "blur" },
    ],
    userPass: [
        {
            required: true,
            message: "请输入登录密码",
            trigger: "blur",
        },
        { pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{6,}$/ , message: "密码必须包含字母和数字,并且最小长度6位", trigger: "blur" },
    ],
    nickName: [
        {
            required: true,
            message: "请输入真实姓名",
            trigger: "blur",
        },
        { pattern: /^[\u4e00-\u9fa5]{0,}$/ , message: "真实姓名必须是汉字", trigger: "blur" },
    ],
    userMobile: [
        {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
        },
        { pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/ , message: "请输入正确的手机号", trigger: "blur" },
    ],
    userEmail: [
        {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
        },
        { pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ , message: "请输入正确的邮箱", trigger: "blur" },
    ],
    
});

// 商户注册
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid, fields) => {
        if (valid) {
            // 表单校验通过
            userReg(ruleForm).then(res => {
                if(res.data.code == 200) {
                    ElMessage.success('注册成功');
                    // 跳转页面
                    router.push('/login');
                }
                else {
                    ElMessage.error(res.data.msg);
                }
            })
        } else {
            // 表单校验不通过
            console.log("error submit!", fields);
        }
    });
};

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}));
</script>
